using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using ApiTest.Api.Entity;
using System.Linq;
using ApiTest.Api.Db;
using ApiTest.Api.Repository;
using ApiTest.Api.Params;
using Microsoft.Extensions.Configuration;
using ApiTest.Api.Utils;
using Microsoft.AspNetCore.Authorization;
using Admin3000.Backend.Api.Params;

namespace ApiTest.Api.Controllers
{
    // [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class SubclassifyController : ControllerBase
    {
        private IConfiguration _configuration;
        private IRepository<Subclassify> _subclassifyController;
        private TokenParameter _tokenParameter;

        public SubclassifyController(IConfiguration configuration, IRepository<Subclassify> subclassifyController)
        {
            _configuration = configuration;
            _subclassifyController = subclassifyController;
            _tokenParameter = _configuration.GetSection("TokenParameter").Get<TokenParameter>();
        }

        //列出分类列表
        [HttpGet]
        public dynamic Get()
        {
            var subclassify = _subclassifyController.Table.ToList();
            var res = new
            {
                Code = 1000,
                Data = new { Data = subclassify },
                Msg = "获取列表成功^_^"
            };

            return JsonHelper.Serialize(res);
        }


        [HttpGet("{id}")]
        public dynamic Get(int id)
        {
            var subclassify = _subclassifyController.GetById(id);

            return JsonHelper.Serialize(new
            {
                Code = 1000,
                Data = subclassify,
                Msg = "查询指定用户成功"
            });
        }

        [HttpPost]
        public dynamic Post(CreateSubclassify newSubclassify)
        {
            var subclassifyfyname = newSubclassify.Subclassifyfyname.Trim();
            var Subclassify = new Subclassify
            {
                Subclassifyfyname = newSubclassify.Subclassifyfyname,
                ClassifyId = newSubclassify.ClassifyId,
            };
            _subclassifyController.Insert(Subclassify);
            return JsonHelper.Serialize(new
            {
                Code = 1000,
                Data = Subclassify,
                Msg = "添加用户成功"
            });
        }
        [HttpPut("{id}")]
        public dynamic Put(int id, CreateSubclassify updateSubclassify)
        {
            var Subclassifyfyname = updateSubclassify.Subclassifyfyname.Trim();
            var Subclassifyf = _subclassifyController.GetById(id);
            if (Subclassifyf == null)
            {
                return JsonHelper.Serialize(new
                {
                    Code = 104,
                    Data = "",
                    Msg = "查询不到该用户，无法更新，请重试"
                });
            }

            Subclassifyf.Subclassifyfyname = updateSubclassify.Subclassifyfyname;
            // user.IsActived= updateUser.IsActived;

            _subclassifyController.Update(Subclassifyf);

            return JsonHelper.Serialize(new
            {
                Code = 1000,
                Data = Subclassifyf,
                Msg = "更新成功"
            });


        }

        [HttpDelete("{id}")]
        public dynamic Delete(int id)
        {
            _subclassifyController.Delete(id);

            return new
            {
                Code = 1000,
                Data = "",
                Msg = "删除成功"
            };


        }
    }

}
